package com.yango.cmsservice.controller.admin;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yango.cmsservice.entity.Banner;
import com.yango.cmsservice.service.BannerService;
import com.yango.commonutil.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * Banner管理 前端控制器
 * </p>
 *
 * @author LiDingYin
 * @since 2021-07-29
 */
@Api(description = "Banner管理 前端控制器")
@RestController
@RequestMapping("/cmsservice/admin/banner")
public class BannerAdminController {

    @Autowired
    private BannerService bannerService;

    //新增banner
    @CacheEvict(value = "banner", allEntries = true)
    @ApiOperation(value = "新增banner")
    @PostMapping("/saveBanner")
    public Result saveBanner(@ApiParam(name = "banner", value = "banner对象", required = true)
                             @RequestBody Banner banner) {
        //调用方法添加
        boolean save = bannerService.save(banner);
        if (save) {
            return Result.ok().message("新增banner成功!");
        } else {
            return Result.error().message("新增banner失败!");
        }
    }

    //分页查询banner列表
    @ApiOperation(value = "分页查询banner列表")
    @GetMapping("/pageBannerList/{current}/{limit}")
    public Result pageBannerList(@ApiParam(name = "current", value = "当前页", required = true)
                                 @PathVariable long current,
                                 @ApiParam(name = "limit", value = "每页显示记录数", required = true)
                                 @PathVariable long limit) {
        //创建page对象
        Page<Banner> page = new Page<>(current, limit);
        //调用方法查询
        bannerService.page(page, null);
        //获取记录集合
        List<Banner> records = page.getRecords();
        //获取总记录数
        long total = page.getTotal();
        //返回
        return Result.ok().data("total", total).data("bannerList", records);
    }

    //根据id查询
    @ApiOperation(value = "根据id查询")
    @GetMapping("/getBannerById/{id}")
    public Result getBannerById(@ApiParam(name = "id", value = "id", required = true)
                                @PathVariable String id) {
        //查询
        Banner banner = bannerService.getById(id);
        //返回
        if (banner != null) {
            return Result.ok().data("banner", banner);
        } else {
            return Result.error().message("每页查询到banner信息!");
        }
    }

    //根据id修改
    @CacheEvict(value = "banner", allEntries = true)
    @ApiOperation(value = "根据id修改")
    @PutMapping("/updateById/{id}")
    public Result updateById(@ApiParam(name = "id", value = "id", required = true)
                             @PathVariable String id,
                             @ApiParam(name = "banner", value = "banner对象", required = true)
                             @RequestBody Banner banner) {
        banner.setId(id);//设置id
        //调用方法修改
        boolean update = bannerService.updateById(banner);
        if (update) {
            return Result.ok().message("修改banner成功!");
        } else {
            return Result.error().message("修改banner失败!");
        }
    }

    //删除banner
    @CacheEvict(value = "banner", allEntries = true)
    @ApiOperation(value = "删除banner")
    @DeleteMapping("/removeById/{id}")
    public Result removeById(@ApiParam(name = "id", value = "id", required = true)
                             @PathVariable String id) {
        //调用方法删除
        boolean remove = bannerService.removeById(id);
        if (remove) {
            return Result.ok().message("删除banner成功!");
        } else {
            return Result.error().message("删除banner是把你!");
        }
    }

}

